<template>
  <div class="home">
    <div class="login">
      <p class="title">后台管理</p>
      <a-form layout="vertical" :model="formInline" @submit="handleSubmit">
        <a-form-item>
          <a-input v-model:value="formInline.username" placeholder="Username">
            <template #prefix
              ><UserOutlined style="color: rgba(0, 0, 0, 0.25)"
            /></template>
          </a-input>
        </a-form-item>
        <a-form-item>
          <a-input
            v-model:value="formInline.password"
            type="password"
            placeholder="Password"
          >
            <template #prefix
              ><LockOutlined style="color: rgba(0, 0, 0, 0.25)"
            /></template>
          </a-input>
        </a-form-item>
        <div class="but">
          <a-form-item>
            <a-button
              type="primary"
              html-type="submit"
              :disabled="
                formInline.username === '' || formInline.password === ''
              "
            >
              登陆
            </a-button>
          </a-form-item>
        </div>
      </a-form>
    </div>
  </div>
</template>

<script lang="ts">
import { UserOutlined, LockOutlined } from "@ant-design/icons-vue";
import Vue, { defineComponent, reactive, toRefs } from "vue";
import { message } from "ant-design-vue";
import { login } from "@/api/user";
import { useRouter } from "vue-router";

export default defineComponent({
  components: {
    UserOutlined,
    LockOutlined,
  },
  setup(props, context) {
    const router = useRouter();
    const state = reactive({
      formInline: {
        username: "",
        password: "",
      },
    });
    const handleSubmit = (e: any) => {
      login(JSON.stringify(state.formInline)).then((res: any) => {
        if (res.data.token) {
          localStorage.setItem("accessToken", res.data.token);
          message.success("登陆成功");
          router.push("/home").catch(() => {});
        } else {
          message.error("没有返回token");
        }
      });
    };
    return {
      ...toRefs(state),
      handleSubmit,
    };
  },
});
</script>
<style scoped>
.home {
  position: absolute;
  width: 100%;
  height: 100%;
  display: flex;
  justify-content: center;
  align-items: center;
  background: url("../../assets/bgimg.png");
  background-size: 100% 100%;
}
.login {
  padding: 3%;
  width: 440px;
  height: 300px;
  border: 1px solid rgb(10, 72, 165);
}
.but {
  width: 100%;
  display: flex;
  justify-content: center;
}
.title {
  color: #fff;
  width: 100%;
  font: 0.4rem sans-serif;
  text-align: center;
}
</style>
